﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Doo.WebUI;

namespace Doo.Controls
{
    /* 
     * TODO:0912，缓存结果，即使web服务未进行缓存设置...。
     * TODO:09-10，将Pager移动到此项目中
     * 
     */

    [ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]
    public class Pager : DooControl
    {
        const string _ScriptName = "Doo.WebUI.Pager.script.js";

        #region 设置

        public override string ClientControlType
        {
            get
            {
                return "Doo.Pager";
            }
        }

        public override string ScriptResourceName
        {
            get
            {
                return _ScriptName;
            }
        }

        #endregion

        #region 枚举

        public enum PagerStyle
        {
            Numeric = 0,
            PrevNext = 1,
            Both = 2
        }
        /// <summary>
        /// 分页行为
        /// </summary>
        public enum PagerBehavior
        {
            /// <summary>
            /// 仅回发
            /// </summary>
            PostBack = 1,
            /// <summary>
            /// 仅使用URL
            /// </summary>
            Url = 2,
            /// <summary>
            /// 对搜索引擎友好
            /// 首先尝试回发，然后使用URL
            /// </summary>
            Both = 0
        }

        #endregion

        /// <summary>
        /// 是否使用客户端脚本生成分页
        /// </summary>
        [DefaultValue(true)]
        public bool UseClientScript { get; set; }

        public string OnClientPageIndexChange { get; set; }

        /// <summary>
        /// 是否在初始化时就触发事件
        /// </summary>
        public bool TriggerIndexChangeOnInit { get; set; }

        public Pager()
        {
            //设置默认值等
            this.UseClientScript = true;
            this.PageIndexParameterName = "p";

        }

        /// <summary>
        /// 是否使用Url来导航，使用Url导航将始终触发PageIndexChanged事件
        /// </summary>
        [DefaultValue(PagerBehavior.Both)]
        public PagerBehavior Behavior
        {
            get;
            set;
        }

        /// <summary>
        /// 使用Url分页时指定当前页的参数名称
        /// </summary>
        [DefaultValue("p")]
        public string PageIndexParameterName
        { get; set; }

        #region 属性

        /// <summary>
        /// 当前页[从1开始]
        /// </summary>
        public int PageIndex
        {
            get { var o = ViewState["p"]; return o == null ? 1 : (int)o; }
            set { ViewState["p"] = value < 1 ? 1 : value; }
        }

        /// <summary>
        /// 获取和设置总记录数
        /// </summary>
        public int RecordCount
        {
            get { var o = ViewState["c"]; return o == null ? 0 : (int)o; }
            set { ViewState["c"] = value < 0 ? 0 : value; }
        }

        /// <summary>
        /// 获取和设置每页显示记录数
        /// </summary>
        public int PageSize
        {
            get { var o = ViewState["s"]; return o == null ? 10 : (int)o; }
            set { ViewState["s"] = value < 1 ? 1 : value; }
        }

        /// <summary>
        /// 获取总页数
        /// </summary>
        public int PageCount
        {
            get { return (RecordCount + PageSize - 1) / PageSize; }
        }

        /// <summary>
        /// 获取应该取的行数，同PageSize
        /// </summary>
        public int RowCount
        {
            get { return PageSize; }
        }

        /// <summary>
        /// 获取开始行数
        /// </summary>
        public int RowStart
        {
            get { return (PageIndex - 1) * PageSize; }
        }

        public PagerStyle DisplayStyle { get; set; }

        #endregion

        protected override void SetScriptDescriptor(ScriptControlDescriptor descriptor)
        {
            base.SetScriptDescriptor(descriptor);
            descriptor.AddProperty("style", (int)this.DisplayStyle);
            descriptor.AddProperty("recordcount", this.RecordCount);
            descriptor.AddProperty("pageindex", this.PageIndex - 1);
            descriptor.AddProperty("pagesize", this.PageSize);
            descriptor.AddProperty("behavior", (int)this.Behavior);
            if (!string.IsNullOrEmpty(OnClientPageIndexChange))
            {
                descriptor.AddEvent("pageindexchange", OnClientPageIndexChange);
            }

            if (this.TriggerIndexChangeOnInit) descriptor.AddProperty("triggerchangeoninit", true);

        }

    }
}
